//管理localStorage数据读写
function getStorage() {
  const dataStr = localStorage.getItem('list');

  return JSON.parse(dataStr) || [];
}

function setStorage(item, id) {
  let listData = JSON.parse(localStorage.getItem('list')) || [];

  //有传id时说明是删除某一项
  if (id) {
    listData = listData.filter((item) => {
      return item.id !== id;
    });
  } else {
    //没传id时说明是新增一项
    listData.push(item);
  }

  localStorage.setItem('list', JSON.stringify(listData));

  return listData;
}

export {
  getStorage,
  setStorage
}